// run: $exec < input
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>

int const maxn = 30;
int a[maxn];

int calc(std::string const& s)
{
	int n = s.size(), ret = 0;
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < i; j++) {
			std::string t1 = s.substr(0, j + 1);
			std::string t2 = s.substr(i - j, j + 1);
			if (t1 == t2) ret++;
			else break;
		}
	}
	return ret;
}

std::string construct(int x)
{
	if (x == 1) return "ba";
	std::string ret = "aa";
	x -= 2;
	for (int i = 0; i < x; i++) ret += "ba";
	return ret + "bb";
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		for (int i = 0; i < 26; i++) std::cin >> a[i];
		std::sort(a, a + 26, std::greater<int>());
		int len = 0;
		for (; len < 26; len++) if (!a[len]) break;
		std::sort(a, a + len);
		int ans = a[0] * (a[0] - 1) / 2;
		if (len > 1) {
			std::string s = construct(a[0]);
			ans = std::min(ans, calc(s));
		}
		std::cout << ans << '\n';
	}
}

